import type { Directive, DirectiveBinding } from 'vue';
import pinia from '@/stores';
import useUserStore from '@/stores/modules/user';

// 按钮权限对象
interface ButtonPermission {
  path: string; // 按钮权限路径
}

const userStore = useUserStore(pinia);

const permission: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding<ButtonPermission>) {
    const { value } = binding;
    if (!value) {
      return;
    }

    const permissions = userStore.buttons;
    if (!permissions.includes(value.path)) {
      el.style.display = 'none';
    } else {
      el.style.display = 'auto';
    }
  }
};

export default permission;
